<?php

namespace app\common;

use Firebase\JWT\Key;
use think\Collection;

class Jwt
{
    public static function getUserToken($uid, $scenario)
    {
        $payload = config("jwt.{$scenario}.payload");
        $payload['user'] = ['uid' => $uid];
        return \Firebase\JWT\JWT::encode($payload, config("jwt.{$scenario}.key"), 'HS256');
    }

    public static function parseUserInfo($token, $scenario): array
    {
        try {
            $key = new Key(config("jwt.{$scenario}.key"), 'HS256');
            $userInfo = \Firebase\JWT\JWT::decode($token, $key);
        } catch (\Exception $exception) {
//            return [false, $exception->getMessage()];
            return [false, 'token 解析错误'];
        }
        return [true, Collection::make($userInfo)->toArray()];
    }
}